package com.hj.ct.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.hj.ct.App;
import com.hj.ct.activity.BaseActivity;
import com.hj.ct.logic.BaseLogic.OnHandlerResponse;
import com.hj.ct.view.CustomDialog;

abstract public class BaseFragment extends Fragment implements OnHandlerResponse {
	private CustomDialog progressDialog;

	public void toast(String str) {
		Toast.makeText(App.getContext(), str, Toast.LENGTH_LONG).show();
	}

	public void handlerResponse(final int aciton, final int code, final Object... obj) {
		new Handler(Looper.getMainLooper()).post(new Runnable() {
			public void run() {
				handlerUI(aciton, code, obj);
			}
		});
	}

	abstract public void handlerUI(int action, int code, Object... obj);

	public void showProgressDialog(String str) {
		if (progressDialog == null) {
			progressDialog = new CustomDialog(getActivity());
		}
		progressDialog.setMessage(str);
		if (!progressDialog.isShowing()) {
			progressDialog.show();
		}
	}

	public void dimissDialog() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	/**
	 * 隐藏输入法（根据activity当前焦点所在控件的WindowToken）
	 */
	public static void hideSoftInput(Activity activity, View editText) {
		View view;
		if (editText == null) {
			view = activity.getCurrentFocus();
		} else {
			view = editText;
		}

		if (view != null) {
			InputMethodManager inputMethod = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
			inputMethod.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}

	public void showReLoginDialog() {
		((BaseActivity) getActivity()).showReLoginDialog();
	}
}
